package com.example.ice.fittime.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.ice.fittime.R;
import com.example.ice.fittime.entry.Banners;
import com.example.ice.fittime.fragment.SocialFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static com.example.ice.fittime.R.id.imageView;

/**
 * Created by Administrator on 2016/10/15.
 */

public class ActPagerAdapter extends PagerAdapter {
    private Context context;
    private List<Banners> list;
    private List<ImageView> lvList ;
    private boolean isBack;

    public ActPagerAdapter(Context context, List<Banners> list, SocialFragment fragment) {
        this.context = context;
        this.list = list;
        lvList = new ArrayList<>();
        if (list.size()<3) {
            for (int i = 0; i < 2; i++) {
                this.list.addAll(list);
            }
        }
        for (Banners banners : this.list) {
            ImageView imageView = new ImageView(context);
            Glide.with(fragment).load(banners.getImage_url()).placeholder(R.mipmap.top_holder).into(imageView);
            lvList.add(imageView);
        }
    }

    @Override
    public int getCount() {
        return 2<<23;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view.equals(((View) object));
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int newPosition = position % lvList.size();//计算实际应该加在数据源中的第几个条目

        ImageView imageView = lvList.get(newPosition);//小心越界

        if (imageView.getParent() == null) {
            container.addView(imageView);
        }else{
            isBack = true;//标记为先添加后移除
        }
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (!isBack) {//如果是正常的从右往左滑的情况才执行这行代码
            container.removeView((View) object);
        }
        isBack = false;//不管什么情况,最后这个值都得设置为 false
    }
}
